﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

using System.Data;
using Jzy.Model.Project;
using Frame.Lib.Core.Base;
using Frame.Lib.Utilities;

namespace Jzy.DAL.Project
{
    public class PrjBugDAL : BaseDAL
    {
        /// <summary>
        /// 获得某个人登记的bug列表，不包括已归档项目
        /// </summary>
        /// <param name="creatorID"></param>
        /// <returns></returns>
        public List<PrjBugInfo> GetRegistList(int status, int creatorID)
        {
            string sql = "select *,(select Name from " + OTableName.Project + " where ID = " + OTableName.PrjBug + ".PrjID) as PrjName from " + 
                         OTableName.PrjBug + " where CreatorID = @p0 and PrjID in (select ID from " + 
                         OTableName.Project + " where Status <>" + (int)PrjStatus.归档 + " and Status <> " + (int)PrjStatus.作废 + ")";
            if (status != -1)
            {
                sql += " and Status = " + status;
            }
            sql += " order by Status,InputTime desc";
            List<PrjBugInfo> dt = dbOper.Query<PrjBugInfo>(sql, creatorID);
            return dt;
        }

        public DataTable GetNoArchivePrj()
        {
            string sql = "select ID,Name,PMID,PMName from " + OTableName.Project + " where Status <> " + (int)PrjStatus.归档 + 
                         " and Status <> " + (int)PrjStatus.作废;
            DataTable dt = dbOper.Query(sql);
            return dt;
        }

        public List<PrjBugInfo> GetDoList(int status, int userID)
        {
            string sql = "select *,(select Name from " + OTableName.Project + " where ID = " + OTableName.PrjBug + ".PrjID) as PrjName from " + 
                         OTableName.PrjBug + " where ReceiveID = @p0 and PrjID in (select ID from " + OTableName.Project + 
                         " where Status <>" + (int)PrjStatus.归档 + " and Status <> " + (int)PrjStatus.作废 + ")";
            if (status != -1)
            {
                sql += " and Status = " + status;
            }
            sql += " order by Status,InputTime desc";
            List<PrjBugInfo> dt = dbOper.Query<PrjBugInfo>(sql, userID);
            return dt;
        }

        public DataTable GetSelectData(string strSelectIDs)
        {
            string sql = "select * from " + OTableName.PrjBug + " where ID in ";
            string paraNames;
            object[] paraValue;

            List<int> lstIDs = StrUtil.SplitToInt(strSelectIDs);
            StrUtil.ParseToParameter(lstIDs, out paraNames, out paraValue);
           
            sql += "(" + paraNames + ")";
            DataTable dt = dbOper.Query(sql, paraValue);
            return dt;
        }

        public List<PrjBugInfo> GetListByProject(string prjID, int status)
        {
            string sql = "select * from " + OTableName.PrjBug + " where PrjID=@p0";
            if (status != -1)
            {
                sql += " and Status = @p1";
            }
            sql += " order by  Status,InputTime desc";

            List<PrjBugInfo> dt = dbOper.Query<PrjBugInfo>(sql, prjID, status);

            return dt;
        }
    }

}
